// pages/goods/card/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize: 1,
    pageNum: 10,
    goodsList: [],
    goodsNum: 0,
    showDialog: false,
    show:false,
    url: "https://erp.yilestar.com/",
  },


  onLoad(options) {
    this.getData()
  },
  onShow() {

  },


  // 获取数据
  getData(e) {
    const self = this;
    const data = {
      pageSize: self.data.pageSize,
      pageNum: self.data.pageNum,
      tokenId: '5d9df059c27602f5a80e2176d671f9cf'
    }

    wx.request({
      url: 'https://erp.yilestar.com/weapp/Carts/index',
      data,
      method: 'post',
      success: (res) => {
        const custId = wx.getStorageSync('custId')
        if (!custId) {
          wx.showToast({
            title: '暂无客户相关数据',
            icon: 'none'
          })
          return;
        }
        if (res.data.data.carts[custId] == undefined) {
          wx.showToast({
            title: '暂无客户相关数据',
            icon: 'none'
          })
          return
        }
        const datas = res.data.data.carts[custId].list
        self.setData({
          goodsList: datas,
          show:true
        })
      }
    })
  },

  // 获取goodsNum
  onChange(e) {
    console.log(e)
    const self = this;
    var num = e.detail;
    let item = e.currentTarget.dataset.item;
    if(num){
      self.addCart(e, item)
    }else{
      self.deleteGoods(item.cartId)
    }
  },

  onPlus(e) {
    console.log(`onPlus: ${e.detail}`)
  },
 
  onFocus(e) {
    console.log(e.detail.value)
    const self = this;
    self.setData({
      showDialog: true
    })
  },

  delGoods(e){
    this.deleteGoods(e.currentTarget.dataset.cartid)
  },

  // delGoods
  deleteGoods(e) {
    const cartId = e;
    const self = this;
    const url = "https://erp.yilestar.com/weapp/Carts/delCart"
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf',
      id: cartId,
    }
    wx.showModal({
      title: '提示',
      content: '确认删除该商品？',
      showCancel: true,
      cancelText: '点错了',
      confirmText: '确定',
      confirm:()=>{
        wx.request({
          url,
          data,
          method: 'post',
          success: (res) => {
            if (res.data.status == -1) {
              // todo:500报错
              wx.showToast({
                title: '删除失败',
                icon: 'none',
              })
              return;
            }
            wx.showToast({
              title: '删除成功',
              icon: 'none',
            })
            self.setData({
              goodsList: []
            })
            self.getData()
          },
        })
      }
    })
  },
  // 添加到购物清单
  addCart(e, num) {
    const item = e;
    const url = 'https://erp.yilestar.com/weapp/Carts/addCart';
    const self = this;
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf', // wx.getStorageSync("tokenId")
      custId: wx.getStorageSync("custId"), // 客户唯一id ; 
      goodsId: item.goodsId,
      unitName: item.goodsUnit, // 规格
      saleTypeId: item.saleTypeId ? item.saleTypeId : 0, // 选择类别，默认为0
      shopPrice: item.shopPrice,
      unitFlg: item.unit_count, //规格
      buyNum: num // 购买数量
    }
    if (!data.buyNum) {
      return
    }
    wx.request({
      url,
      data,
      method: "post",
      success: (res) => {
        wx.showToast({
          title: '添加成功',
        })
      }
    })

  },
  // 跳转到生成订单页面
  goOrder() {
    this.getData()
    wx.setStorageSync('cartList', this.data.goodsList)
    wx.navigateTo({
      url: '../../order/newOrder/index',
    })
  },
})